package utils

import (
	"strconv"
	"time"
)

const (
	CacheExpire = 86400
	FormatShort = "2006-01-02"
	FormatLong  = "2006-01-02 15:04:05"
)

// 当前时间戳
func NowTime() int64 {
	return time.Now().Unix()
}

// 当前日期格式 2020-06-01
func NowTimes() string {
	return time.Now().Format(FormatShort)
}

// 当前日期格式 2020-06-01 15:04:05
func NowTimel() string {
	return time.Now().Format(FormatLong)
}

// 今日0点时间戳
func TodayTimestamp() int64 {
	t := time.Unix(NowTime(), 0).Format(FormatShort)
	loc, _ := time.LoadLocation("Local")
	theTime, _ := time.ParseInLocation(FormatShort, t, loc)
	return theTime.Unix()
}

// 今日0点日期格式 offset -1 = 昨日
func TodayTimestamps(offset int64) string {
	return time.Unix(NowTime()+CacheExpire*offset, 0).Format(FormatShort)
}

// 时间戳转日期格式 1590940800 = 2020-06-01
func TimestampToDate(timestamp int64) string {
	return time.Unix(timestamp, 0).Format(FormatShort)
}

// 时间戳转日期格式 1590982800 = 2020-06-01 11:40:00
func TimestampToDates(timestamp string) string {
	stamp, _ := strconv.ParseInt(timestamp, 10, 64)
	return time.Unix(stamp, 0).Format(FormatLong)
}

// 日期格式转时间戳 2020-06-01 = 1590940800
func DateToTimestamp(date string) int64 {
	loc, _ := time.LoadLocation("Local")
	theTime, _ := time.ParseInLocation(FormatShort, date, loc)
	return theTime.Unix()
}

// 日期格式转时间戳 2020-06-01 11:40:00  = 1590982800
func DateToTimestamps(date string) int64 {
	loc, _ := time.LoadLocation("Local")
	theTime, _ := time.ParseInLocation(FormatLong, date, loc)
	return theTime.Unix()
}
